package net.yla.smartbus.module.controller.web.ba.vehicle;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.core.util.ExcelUtils;
import net.yla.smartbus.module.ba.vehicle.common.ExcelData;
import net.yla.smartbus.module.ba.vehicle.dto.VehicleDto;
import net.yla.smartbus.module.ba.vehicle.form.VehicleForm;
import net.yla.smartbus.module.ba.vehicle.query.VehicleDeleteQuery;
import net.yla.smartbus.module.ba.vehicle.query.VehicleQuery;
import net.yla.smartbus.module.ba.vehicle.service.VehicleService;
import net.yla.smartbus.module.ba.vehicle.utils.MyExcelUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

/**
 * @author 默苍离
 * @date 2021/6/17 17:11
 * <p>
 * ******************************************
 * @annotation:
 */

@RestController
@RequestMapping("/api/web/taxi/ba")
public class VehicleController extends BaseController {

    private final VehicleService vehicleService;

    public VehicleController(VehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }

    /**
     * 根据 id 查询
     *
     * @param vehicleId
     * @return
     */
    @PostMapping("/vehicle/getOne")
    public ApiResult getOne(Integer vehicleId) {
//        rolesService.userRoleEvaluation(UserRoleEnum.VEHICLE_SELECT, getToken(), getIpAddress());
        VehicleDto list = vehicleService.getOne(vehicleId);
        return success(list);
    }


    /**
     * 根据 查询条件 查询 所有 车辆
     *
     * @param query
     * @return
     */
    @PostMapping("/vehicle/getList")
    public ApiResult getList(@RequestBody VehicleQuery query) {
//        rolesService.userRoleEvaluation(UserRoleEnum.VEHICLE_SELECT, getToken(), getIpAddress());
        PageBean<VehicleDto> list = vehicleService.getListByPage(query);
        return success(list);
    }


    /**
     * 根据ids 删除车辆
     *
     * @param ids
     * @return
     */
    @PostMapping("/vehicle/deletes")
    public ApiResult deletes(@RequestBody VehicleDeleteQuery ids) {
//        rolesService.userRoleEvaluation(UserRoleEnum.VEHICLE_DELETE, getToken(), getIpAddress());
        vehicleService.delete(ids);
        return success(success());
    }


    /**
     * 下载excel模板
     */
    @GetMapping("/vehicle/exportTemplate")
    @CrossOrigin
    public ApiResult exportTemplate(HttpServletResponse response) throws IOException, InvalidFormatException {

        String path = "/exportTemplate/车辆导入模板.xls";
        String fileName = "车辆导入模板";
        ExcelUtils.exportTemplate(response, path, fileName);

        return success(success());

    }


    /**
     * 导出车辆信息
     */
    @GetMapping("/vehicle/exportVehicles")
    public ApiResult exportVehicles(HttpServletResponse response, VehicleQuery query) throws IOException {
        ExcelData excelData = vehicleService.exportVehicles(query);
        MyExcelUtil.exportExcel(response, excelData);
        return success(success());

    }


    /**
     * 导入车辆信息
     */
    @PostMapping("/vehicle/importVehicles")
    public ApiResult importVehicles(MultipartFile file) throws IOException, ParseException {
        Integer integer = vehicleService.insertByImport(file);
        if (integer == 400) {
            return error(400, "数据已存在");
        }
        return success(success());
    }


    /**
     * 新增车辆
     *
     * @param vehicle
     * @return
     */
    @PostMapping("/vehicle/addVehicle")
    public ApiResult addVehicle(@RequestBody VehicleForm vehicle) {

        Integer insert = vehicleService.insert(vehicle);
        if (insert == 400) {
            return error(400, "数据已存在");
        }
        return success(success(insert));

    }

}